﻿Imports System.Web.UI.WebControls

'© 2004 - 2008 ActiveModules, Inc. All Rights Reserved
Namespace DotNetNuke.Modules.ActiveForums
    Partial Public Class af_topicrating
        Inherits ForumBase
        Public RatingClass As String = "rating0"
#Region "Private Members"
        Private _Rating As Integer = -1
        Private _Enabled As Boolean = False
#End Region
#Region "Controls"
        Protected WithEvents Rate1 As New ImageButton
        Protected WithEvents Rate2 As New ImageButton
        Protected WithEvents Rate3 As New ImageButton
        Protected WithEvents Rate4 As New ImageButton
        Protected WithEvents Rate5 As New ImageButton

#End Region
#Region "Public Properties"
        Public Property Rating() As Integer
            Get
                Return _Rating
            End Get
            Set(ByVal value As Integer)
                _Rating = value
            End Set
        End Property
        Public Property Enabled() As Boolean
            Get
                Return _Enabled
            End Get
            Set(ByVal value As Boolean)
                _Enabled = value
            End Set
        End Property
#End Region
#Region "Event Handlers"
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            RenderRating()
            Dim sRating As String = "function afchangerate(rate){var rd = document.getElementById('ratingdiv');rd.className=rate;};"
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "afratescript", sRating, True)
        End Sub

        Private Sub Rate1_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Rate1.Click
            DataProvider.Instance.Topics_AddRating(TopicId, UserId, 1, String.Empty, Request.UserHostAddress.ToString)
        End Sub

        Private Sub Rate2_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Rate2.Click
            DataProvider.Instance.Topics_AddRating(TopicId, UserId, 2, String.Empty, Request.UserHostAddress.ToString)
        End Sub

        Private Sub Rate3_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Rate3.Click
            DataProvider.Instance.Topics_AddRating(TopicId, UserId, 3, String.Empty, Request.UserHostAddress.ToString)
        End Sub

        Private Sub Rate4_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Rate4.Click
            DataProvider.Instance.Topics_AddRating(TopicId, UserId, 4, String.Empty, Request.UserHostAddress.ToString)
        End Sub

        Private Sub Rate5_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Rate5.Click
            DataProvider.Instance.Topics_AddRating(TopicId, UserId, 5, String.Empty, Request.UserHostAddress.ToString)
        End Sub
        Private Sub cbRating_Callback(ByVal sender As Object, ByVal e As Modules.ActiveForums.Controls.CallBackEventArgs) Handles cbRating.Callback
            If e.Parameters.Length > 0 Then
                Dim rate As Integer = CInt(e.Parameter)
                If rate >= 1 And rate <= 5 Then
                    DataProvider.Instance.Topics_AddRating(TopicId, UserId, rate, String.Empty, Request.UserHostAddress.ToString)
                End If
            End If
            Rating = -1
            RenderRating()
            plhRating.RenderControl(e.Output)
        End Sub
#End Region
#Region "Private Methods"
        Private Sub RenderRating()
            If Rating = -1 Then
                Rating = DataProvider.Instance.Topics_GetRating(TopicId)
            End If
            RatingClass = "rating" & Rating.ToString
            plhRating.Controls.Clear()
            Rate1.Attributes.Add("onmouseover", "afchangerate('rating1');")
            Rate1.Enabled = Enabled
            Rate2.Attributes.Add("onmouseover", "afchangerate('rating2');")
            Rate2.Enabled = Enabled
            Rate3.Attributes.Add("onmouseover", "afchangerate('rating3');")
            Rate3.Enabled = Enabled
            Rate4.Attributes.Add("onmouseover", "afchangerate('rating4');")
            Rate4.Enabled = Enabled
            Rate5.Attributes.Add("onmouseover", "afchangerate('rating5');")
            Rate5.Enabled = Enabled

            Dim lit As New Literal
            lit.Text = "<div class=""" & RatingClass & """ id=""ratingdiv"" onmouseout=""this.className='" & RatingClass & "'"">"
            plhRating.Controls.Add(lit)
            Rate1.ID = "Rate1"
            Rate1.CausesValidation = False
            Rate1.Width = 13
            Rate1.Height = 14
            Rate1.ImageUrl = "~/DesktopModules/ActiveForums/images/spacer.gif"
            plhRating.Controls.Add(Rate1)
            Rate2.ID = "Rate2"
            Rate2.CausesValidation = False
            Rate2.Width = 14
            Rate2.Height = 14
            Rate2.ImageUrl = "~/DesktopModules/ActiveForums/images/spacer.gif"
            plhRating.Controls.Add(Rate2)
            Rate3.ID = "Rate3"
            Rate3.CausesValidation = False
            Rate3.Width = 14
            Rate3.Height = 14
            Rate3.ImageUrl = "~/DesktopModules/ActiveForums/images/spacer.gif"
            plhRating.Controls.Add(Rate3)
            Rate4.ID = "Rate4"
            Rate4.CausesValidation = False
            Rate4.Width = 14
            Rate4.Height = 14
            Rate4.ImageUrl = "~/DesktopModules/ActiveForums/images/spacer.gif"
            plhRating.Controls.Add(Rate4)
            Rate5.ID = "Rate5"
            Rate5.CausesValidation = False
            Rate5.Width = 14
            Rate5.Height = 14
            Rate5.ImageUrl = "~/DesktopModules/ActiveForums/images/spacer.gif"

            plhRating.Controls.Add(Rate5)
            lit = New Literal
            lit.Text = "</div>"
            plhRating.Controls.Add(lit)
            If UseAjax() Then
                Rate1.OnClientClick = "af_rateTopic(1);return false;"
                Rate2.OnClientClick = "af_rateTopic(2);return false;"
                Rate3.OnClientClick = "af_rateTopic(3);return false;"
                Rate4.OnClientClick = "af_rateTopic(4);return false;"
                Rate5.OnClientClick = "af_rateTopic(5);return false;"
                AddRatingScript()
            End If
        End Sub
        Private Sub AddRatingScript()
            Dim sb As New Text.StringBuilder
            sb.Append("function af_rateTopic(rate){" & cbRating.ClientID & ".Callback(rate);};")
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "afrate", sb.ToString, True)
        End Sub
#End Region
    End Class
End Namespace
